package online.foxnull2.foxmall.member.controller;

import online.foxnull2.common.exception.BizCodeEnum;
import online.foxnull2.common.utils.R;
import online.foxnull2.foxmall.member.entity.MemberEntity;
import online.foxnull2.foxmall.member.exception.EmailExistException;
import online.foxnull2.foxmall.member.exception.UsernameExistException;
import online.foxnull2.foxmall.member.service.MemberService;
import online.foxnull2.foxmall.member.utils.PageUtils;
import online.foxnull2.foxmall.member.vo.MemberLoginVo;
import online.foxnull2.foxmall.member.vo.MemberRegisterVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.Map;


/**
 * 会员
 *
 * @author foxnull2
 */
@RestController
@RequestMapping("member/member")
public class MemberController {

    private MemberService memberService;

    @Autowired
    public void setMemberService(MemberService memberService) {
        this.memberService = memberService;
    }

    @PostMapping("/register")
    public R register(@RequestBody MemberRegisterVo memberRegisterVo) {
        try {
            memberService.register(memberRegisterVo);
        } catch (EmailExistException e) {
            return R.error(BizCodeEnum.EMAIL_EXIST_EXCEPTION.getCode(),
                    BizCodeEnum.EMAIL_EXIST_EXCEPTION.getMsg());
        } catch (UsernameExistException e) {
            return R.error(BizCodeEnum.USERNAME_EXIST_EXCEPTION.getCode(),
                    BizCodeEnum.USERNAME_EXIST_EXCEPTION.getMsg());
        }
        return R.ok();
    }

    @PostMapping("/login")
    public R login(@RequestBody MemberLoginVo memberLoginVo) {
        MemberEntity memberEntity = memberService.login(memberLoginVo);
        if (!ObjectUtils.isEmpty(memberEntity)) {
            return R.ok().setObj(memberEntity);
        } else {
            return R.error(BizCodeEnum.USERNAME_OR_PASSWORD_INVALID_EXCEPTION.getCode(),
                    BizCodeEnum.USERNAME_OR_PASSWORD_INVALID_EXCEPTION.getMsg());
        }
    }

    /**
     * 列表
     */
    @RequestMapping("/list")
    public R list(@RequestParam Map<String, Object> params) {
        PageUtils page = memberService.queryPage(params);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    public R info(@PathVariable("id") Long id) {
        MemberEntity member = memberService.getById(id);

        return R.ok().put("member", member);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    public R save(@RequestBody MemberEntity member) {
        memberService.save(member);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    public R update(@RequestBody MemberEntity member) {
        memberService.updateById(member);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    public R delete(@RequestBody Long[] ids) {
        memberService.removeByIds(Arrays.asList(ids));

        return R.ok();
    }

}
